﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class CtrlCompany : BaseControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Control.SelectedValue == "")
                {
                    var companyBLL = new Sys_CompanyBLL();
                    var user = (Sys_User)Session["SysUserInfo"];
                    var dt = companyBLL.GetCompanyFatherAndChild(user.USER_COMPANY);
                    Control.Items.Clear();
                    Control.Items.Add(new ListItem("---请选择---", "-1"));
                    foreach (DataRow item in dt.Rows)
                    {
                        Control.Items.Add(new ListItem(Convert.ToString(item["CMY_NAME"]), Convert.ToString(item["CMY_ID"])));
                    }
                    if (user.USER_LOGINNAME == "SUPER")
                    {
                        Control.SelectedValue = lognUser.USER_COMPANY.ToString();//"-1";
                    }
                    else
                    {
                        Control.SelectedValue = user.USER_COMPANY.ToString();
                    }
                }
            }
        }

        /// <summary>
        /// 选择的公司ID
        /// </summary>
        public decimal Value
        {
            set
            {
                if (Control.SelectedValue == "")
                {
                    var companyBLL = new Sys_CompanyBLL();
                    var user = (Sys_User)Session["SysUserInfo"];
                    var dt = companyBLL.GetCompanyFatherAndChild(user.USER_COMPANY);
                    Control.Items.Clear();
                    Control.Items.Add(new ListItem("---请选择---", "-1"));
                    foreach (DataRow item in dt.Rows)
                    {
                        Control.Items.Add(new ListItem(Convert.ToString(item["CMY_NAME"]), Convert.ToString(item["CMY_ID"])));
                    }
                    if (user.USER_LOGINNAME == "SUPER")
                    {
                        Control.SelectedValue = "-1";
                    }
                    else
                    {
                        Control.SelectedValue = user.USER_COMPANY.ToString();
                    }
                }
                Control.SelectedValue = value.ToString();
            }
            get
            {
                return Convert.ToDecimal(Control.SelectedValue == "" ? ((Sys_User)Session["SysUserInfo"]).USER_COMPANY.ToString() : Control.SelectedValue);
            }
        }

        /// <summary>
        /// 选择的公司名称
        /// </summary>
        public string Text
        {
            get
            {
                return Control.SelectedItem.Text;
            }
        }

        public DropDownList Control
        {
            get
            {
                return FORM_COMPANY_ID;
            }
        }

        public Unit Width
        {
            get
            {
                return FORM_COMPANY_ID.Width;
            }
            set
            {
                FORM_COMPANY_ID.Width = value;
            }
        }
    }
}